﻿using System;
using ASmile.Extend;
using ASmile.Helpers;
using System.Collections.Generic;
using System.IO;

namespace ASmile.Extern.Lib
{
    static class Config
    {
        static Config()
        {
            LoadConfig();
        }

        public static AppSetings Settings { get; private set; }
        /// <summary>
        /// 载入配置文件中的配置，默认：Conf-ExternApp.json
        /// </summary>
        public static void LoadConfig(string cfgFile = null)
        {
            if (cfgFile.IsEmpty())
            {
                cfgFile = "Conf-ExternApp.json";
            }
            var fullFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, cfgFile);
            var jsonText = FileHelper.TextFileRead(fullFile);
            Settings = JsonHelper.JsonToObject<AppSetings>(jsonText);
        }

        public static string GetCustom(string key)
        {
            if (Settings?.Custom == null) return null;

            if (!Settings.Custom.TryGetValue(key, out string value))
            {
                return null;
            }
            return value;
        }

        public class AppSetings
        {
            /// <summary>
            /// 监听地址 或 客户端访问地址
            /// 例如：http://*:2001/data/
            /// </summary>
            public string Url { set; get; }
            /// <summary>
            /// 监听端口，指定端口可不配置url
            /// </summary>
            public int ListenPort { set; get; } = 2001;
            /// <summary>
            /// 响应处理对象
            /// </summary>
            public string[] ResponseObjects { set; get; }
            ///// <summary>
            ///// 其它自定义配置
            ///// </summary>
            //public Dictionary<string, string> Custom { set; get; }

            /// <summary>
            /// 其它自定义配置
            /// </summary>
            public DictExtend Custom { set; get; }
        }
    }
}